Shiny Dashboards
Welcome

Colin Rundel

Welcome

Instructor


Colin Rundel

Associate Professor of the Practice

Department of Statistical Science

Duke University

rundel.github.io

Teaching Assistants

  • Garrick Aden-Buie (Posit)
  • Maya Gans
  • Katherine Simeon

Introduce yourself

We wont go around the room, but take the next couple of minutes to introduce yourself to your neighbors.

Some suggested topics,

  • Name

  • Where you are coming from

  • Why you are interested in learning Shiny

05:00

Workshop materials



Schedule (roughly)

Time Activity
09:00 - 09:30 Welcome
09:30 - 10:30 flexdashboard
10:30 - 11:00 Coffee break
11:00 - 12:30 flexdashboard
12:30 - 13:30 Lunch break
13:30 - 14:30 Shinydashboard
14:30 - 15:00 Theming
15:00 - 15:30 Coffee break
15:30 - 16:00 bslib
16:00 - 16:30 Publishing
16:30 - 17:00 Wrap-up

WiFi



Username: Posit Conf 2023

Password: conf2023

Code of Conduct

All details are available at https://posit.co/code-of-conduct/. Please review them carefully.

You can report Code of Conduct violations in person (any posit::conf staff ), by email (codeofconduct@posit.co), or by phone (844-448-1212). Please see the policy linked above for contact information.

Other useful info

  • There are gender-neutral bathrooms located are among the Grand Suite Bathrooms.

  • There are two meditation/prayer rooms: Grand Suite 2A and Grand Suite 2B.

    • Open Sunday - Tuesday 7:30 a.m. - 7:00 p.m., Wednesday 8:00 a.m. - 6:00 p.m.
  • The lactation room is located in Grand Suite 1.

    • Open Sunday - Tuesday 7:30 a.m. - 7:00 p.m., Wednesday 8:00 a.m. - 6:00 p.m.
  • Participants who do not wish to be photographed have red lanyards; please note everyone’s lanyard colors before taking a photo and respect their choices.

Asking for help (Stickies)

I’m stuck

I’m working

I’m done


I have a general question

Other communication (Discord)

You should have received an email with an invitation and instructions for joining the conference’s discord server.

This workshop has a private channel under Workshops,

#shiny-dashboards

This is a great place to ask questions, post resources, memes, or most anything else before, during, and after the workshop.

Computational Environment

RStudio Cloud

You can use the following link to join the workshops RStudio cloud space,

bit.ly/conf2023_shiny_dashboard


Once you have joined you can then select the shiny-dashboard assignment,

which should then create a copy of all materials and launch a cloud session for you.

Cloud session

If everything is working you should see something very close to the following,

File organization

Project root:

  • slides/ - all slides and related materials

  • demos/ - sample code for each demo

  • exercises/ - starter code for each exercise

  • exercises/solutions/ - sample solution code for each exercise

  • data/ - example data sets used in demos and exercises

A starting point

Data

(d = read_csv("data/weather.csv"))
# A tibble: 1,344 × 25
   city    state    time                 temp feelslike humidity   dew precip
   <chr>   <chr>    <dttm>              <dbl>     <dbl>    <dbl> <dbl>  <dbl>
 1 Chicago Illinois 2023-09-05 05:00:00  82.1      86.2     67.8  70.4      0
 2 Chicago Illinois 2023-09-05 06:00:00  81.2      85       69.8  70.4      0
 3 Chicago Illinois 2023-09-05 07:00:00  80.4      83.8     71.8  70.4      0
 4 Chicago Illinois 2023-09-05 08:00:00  80.1      83.3     72.2  70.3      0
 5 Chicago Illinois 2023-09-05 09:00:00  79.2      79.2     73.7  70.1      0
 6 Chicago Illinois 2023-09-05 10:00:00  78.2      78.2     73.9  69.2      0
 7 Chicago Illinois 2023-09-05 11:00:00  77        77       73.8  68        0
 8 Chicago Illinois 2023-09-05 12:00:00  75.8      75.8     75.5  67.5      0
 9 Chicago Illinois 2023-09-05 13:00:00  77.7      77.7     70.2  67.3      0
10 Chicago Illinois 2023-09-05 14:00:00  80.6      82.9     62.0  66.4      0
# ℹ 1,334 more rows
# ℹ 17 more variables: precipprob <dbl>, snow <dbl>, snowdepth <dbl>,
#   windgust <dbl>, windspeed <dbl>, winddir <dbl>, pressure <dbl>,
#   visibility <dbl>, cloudcover <dbl>, solarradiation <dbl>,
#   solarenergy <dbl>, uvindex <dbl>, severerisk <dbl>, conditions <chr>,
#   icon <chr>, source <chr>, region <chr>

Demo 01 - A starter app

demos/demo01.R

library(tidyverse)
library(shiny)

ggplot2::theme_set(ggplot2::theme_bw())

d = readr::read_csv(here::here("data/weather.csv"))

d_vars = d |>
  select(where(is.numeric)) |>
  names()

shinyApp(
  ui = fluidPage(
    titlePanel("Weather Forecasts"),
    sidebarLayout(
      sidebarPanel(
        selectInput(
          "city", "Select a city",
          choices = c("Chicago", "Durham", "Sedona", "New York", "Los Angeles")
        ),
        selectInput(
          "var", "Select a variable",
          choices = d_vars, selected = "temp"
        )
      ),
      mainPanel( 
        plotOutput("plot"),
        tableOutput("minmax")
      )
    )
  ),
  server = function(input, output, session) {
    
    d_city = reactive({
      d |>
        filter(city %in% input$city)
    })
    
    output$plot = renderPlot({
      d_city() |>
        ggplot(aes(x=time, y=.data[[input$var]], color=city)) +
        ggtitle(input$var) +
        geom_line()
    })
    
    output$minmax = renderTable({
      d_city() |>
        mutate(
          day = lubridate::wday(time, label = TRUE, abbr = FALSE),
          date = as.character(lubridate::date(time))
        ) |>
        group_by(date, day) |>
        summarize(
          `min` = min(.data[[input$var]]),
          `max` = max(.data[[input$var]]),
          .groups = "drop"
        )
    })
  }
)

Your turn - Exercise 01

Using the code in exercises/ex01.R (same as the previous slide) try running the app in Posit cloud.

Check that you are able successfully run the shiny app and are able to interact with it.

If everything is working try modifying the code:

  • Can you rearrange the elements of the mainPanel() to make things more readable?

  • What would we need to change to allow for multiple cities to be selected and visualized?

05:00